package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.PageDTO;
import com.boss.common.util.PageUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.entity.AgencyExt;
import com.boss.salary.service.IAgencyExtService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
public class AgencyExtController extends BasicinfoBaseController {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(AgencyExtController.class);

    @Autowired
    private IAgencyExtService agencyExtService;

    /**
     * 新增
     * @param agencyExt
     * @return
     */
    @PostMapping("/saveAgencyExt")
    public ApiResultDTO saveAgencyExt(@RequestBody AgencyExt agencyExt){
        try {
            agencyExtService.insertAgencyExt(agencyExt);
            return ApiResultDTO.success(null, "保存成功");
        } catch (Exception e) {
            logger.error("新增 单位信息表发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "新增 单位信息表发生异常");
        }
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteAgencyExt/{id}")
    public ApiResultDTO deleteAgencyExt(@PathVariable("id") String id){
        try {
            agencyExtService.deleteAgencyExt(id);
            return ApiResultDTO.success(null, "删除成功");
        } catch (Exception e) {
            logger.error("删除 单位信息表发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "删除 单位信息表发生异常");
        }
    }
    /**
     * 修改
     * @param agencyExt
     * @return
     */
    @PostMapping("/updateAgencyExt")
    public ApiResultDTO updateAgencyExt(@RequestBody AgencyExt agencyExt){
        try {
            agencyExtService.updateAgencyExt(agencyExt);
            return ApiResultDTO.success(null, "修改成功");
        } catch (Exception e) {
            logger.error("修改 单位信息表发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "修改 单位信息表发生异常");
        }
    }
    /**
     * 分页查询
     * @param offset
     * @param limit
     * @return
     */
    @GetMapping("/queryAgencyExtPage")
    public ApiResultDTO queryAgencyExtPage(@RequestParam(value = "offset") String offset,
                                           @RequestParam(value = "limit") String limit){

        try {
            Map<String, Object> parameters = new HashMap<>(8);
            parameters.put("offset", offset);
            parameters.put("limit", limit);
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTO(parameters);
            PageDTO<AgencyExt> agencyExtPageDTO = agencyExtService.queryAgencyExtPage(paginationDTO);
            return ApiResultDTO.success(agencyExtPageDTO,null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }
}
